/**
 * Definition for a binary tree node.
 * function TreeNode(val, left, right) {
 *     this.val = (val===undefined ? 0 : val)
 *     this.left = (left===undefined ? null : left)
 *     this.right = (right===undefined ? null : right)
 * }
 */
/**
 * @param {TreeNode} root
 * @return {void} Do not return anything, modify root in-place instead.
 */
 var flatten = function(root) {
    if(!root){
        return root;
    }
    let head=new TreeNode(0,null,null);
    let node=head;
    function digui(root){
        if(!root){
            return;
        }
        node.right=new TreeNode(root.val,null,null);
        node.left=null;
        node=node.right;
        console.log(node);
        digui(root.left);
        digui(root.right);
    }
    digui(root);
    return head.right;
};
 var flatten = function(root) {
    if(!root){
        return root;
    }
    let result=[];
    function digui(root){
        if(!root){
            return;
        }
        result.push(root);
        digui(root.left);
        digui(root.right);
    }
    digui(root);
    let length=result.length;
    let i=0;
    for(;i<length-1;i++){
        result[i].left=null;
        result[i].right=result[i+1];
    }
    result[i].left=null;
    result[i].right=null;
    return root;
};